package com.sakura.flowers.controller;

import cn.hutool.db.sql.Order;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sakura.flowers.entity.*;
import com.sakura.flowers.service.*;
import com.sakura.flowers.util.R;
import com.sakura.flowers.vo.CategoryVO;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : Sakura
 * @Date : 2024/2/21 9:53
 * @Description :   echarts数据分析
 */
@RestController
@RequestMapping("/api/echarts")
//解决跨域问题
@CrossOrigin
public class EchartsController {

    @Resource
    private CategoryService categoryService;

    @Resource
    private FlowerService flowerService;

    @Resource
    private OrderFlowerService orderFlowerService;

    @Resource
    private OrdersService orderService;

    @Resource
    private UserService userService;
    @GetMapping("/one")
    public R one() {
        List<Category> categoryList = categoryService.list();
        List<CategoryVO> voList = new ArrayList<CategoryVO>();
        for (Category category : categoryList) {
            LambdaQueryWrapper<Flower> flowerWrapper = new LambdaQueryWrapper<>();
            flowerWrapper.eq(Flower::getCategoryId, category.getId());
            List<Flower> flowerList = flowerService.list(flowerWrapper);
            CategoryVO vo = new CategoryVO();
            vo.setName(category.getName());
            vo.setValue(flowerList.size());
            voList.add(vo);
        }
        return R.success(voList);
    }

    @GetMapping("/two")
    public R two() {
        List<OrderFlower> list = orderFlowerService.list();
        List result = new ArrayList<>();
        Map<Long, List<OrderFlower>> collect = list.stream().collect(Collectors.groupingBy(OrderFlower::getFlowerId));
        collect.forEach((k, v) -> {
            String name = flowerService.getById(k).getName();
            Integer total = 0;
            for (OrderFlower orderFlower : v) {
                total+=orderFlower.getNum();
            }
            List child = new ArrayList<>();
            child.add(name);
            child.add(total);
            result.add(child);
        });
        result.sort((o1, o2) -> {
            List o3 = (List) o1;
            List o4 = (List) o2;
            Integer value1 = (Integer) o3.get(1);
            Integer value2 = (Integer) o4.get(1);
            return value2 - value1;
        });

        return R.success(result);
    }

    @GetMapping("/three")
    public R three() {
//        首先获取近七天的日期
        List<String> recentSevenDays = getRecentSevenDays();
        List<Long> numList = new ArrayList<>();
        Map<String, List> result = new HashMap<>();
        result.put("xAxis", recentSevenDays);

        LocalDate currentDate = LocalDate.now();
        for (int i = 6; i >= 0; i--) {
            LocalDate localDate = currentDate.minusDays(i);
//            localDate
            LocalDateTime startOfDay = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endOfDay = LocalDateTime.of(localDate, LocalTime.MAX);
            LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.between(Orders::getCreateTime, startOfDay, endOfDay);
            long count = orderService.count(queryWrapper);
            numList.add(count);
        }
        result.put("yAxis", numList);
        return R.success(result);
    }

    @GetMapping("/four")
    public R four() {
//        首先获取近七天的日期
        List<Long> numList = new ArrayList<>();
        Map<String, List> result = new HashMap<>();
        List<String> recentSevenDays = getRecentSevenDays();
        LocalDate currentDate = LocalDate.now();

        for (int i = 6; i >= 0; i--) {
            LocalDate localDate = currentDate.minusDays(i);
//            localDate
            LocalDateTime startOfDay = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endOfDay = LocalDateTime.of(localDate, LocalTime.MAX);
            LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.between(User::getCreateTime, startOfDay, endOfDay);
            long count = userService.count(queryWrapper);
            numList.add(count);
        }
        result.put("xAxis", recentSevenDays);
        result.put("yAxis", numList);
        return R.success(result);
    }
    /**
     * 获取近七天日期
     * @param :
     * @return List<String>
     * @author Sakura
     * @date 2024/1/9 15:56
     */
    public static List<String> getRecentSevenDays() {
        List<String> sevenDays = new ArrayList<>();
        LocalDate currentDate = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MM-dd");

        for (int i = 6; i >= 0; i--) {
            sevenDays.add(currentDate.minusDays(i).format(formatter));
        }

        return sevenDays;
    }

    public static void main(String[] args) {
        List<OrderFlower> orderFlowerList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            OrderFlower orderFlower = new OrderFlower();
            if (i < 5) {
                orderFlower.setId(1l);
                orderFlower.setFlowerId(2l);
                orderFlower.setNum(2);
            } else {
                orderFlower.setId(2l);
                orderFlower.setFlowerId(1l);
                orderFlower.setNum(5);
            }
            orderFlowerList.add(orderFlower);
        }
        Map<Object, List<OrderFlower>> collect = orderFlowerList.stream().collect(Collectors.groupingBy(OrderFlower::getFlowerId));
        collect.forEach((k, v) -> {
            System.out.println(k);
            System.out.println(v);
        });
    }
}
